package com.jeewms.www.tms.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.blankj.utilcode.util.LogUtils;
import com.jeewms.www.tms.constance.Constance;

import pub.devrel.easypermissions.EasyPermissions;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.ACCESS_NETWORK_STATE;
import static android.Manifest.permission.ACCESS_WIFI_STATE;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.CHANGE_WIFI_STATE;
import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.READ_CALL_LOG;
import static android.Manifest.permission.READ_CONTACTS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.REQUEST_INSTALL_PACKAGES;
import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

/**
 * Created by 13799 on 2019/9/19
 */

public class PermissionUtils {

    private static String[] permissions = {WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA, ACCESS_FINE_LOCATION,
            ACCESS_COARSE_LOCATION, ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, READ_PHONE_STATE, READ_CONTACTS,
            READ_CALL_LOG, ACCESS_NETWORK_STATE, SYSTEM_ALERT_WINDOW, REQUEST_INSTALL_PACKAGES};
   /**
   *获取全部权限
   *@author qiancheng
   *creat by 2019/8/20 0:31
   **/
    public static void getAllPermission(Activity activity){
        String[] parms = {WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA, ACCESS_FINE_LOCATION,
                ACCESS_COARSE_LOCATION, ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, READ_PHONE_STATE, READ_CONTACTS,
                READ_CALL_LOG, ACCESS_NETWORK_STATE, SYSTEM_ALERT_WINDOW, REQUEST_INSTALL_PACKAGES,INTERNET};
        checkPermissions(activity,0,parms);
    }

    /**
     *检查是否获取相机权限
     *@author qiancheng
     *creat by 2019/8/19 23:43
     **/
    public static boolean checkCameraPermissions(Activity activity){
        return checkPermissions(activity, Constance.CAMERAREQUESTCODE,CAMERA);
    }
    
    /**
    *获取权限
    *@author qiancheng
    *creat by 2019/8/19 23:42code
    **/
    public static boolean checkPermissions(Activity activity, int requestCode, String... parms) {
        if (EasyPermissions.hasPermissions(activity, parms)) {
            return true;
        } else {
            LogUtils.i("获取权限");
            EasyPermissions.requestPermissions(activity, "点击确认进行权限授权", requestCode, parms);
            return false;
        }
    }



//    //      旧版动态权限添加
//    //添加权限
//    private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
//    private static String[] permissions = {WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, CAMERA, ACCESS_FINE_LOCATION,
//            ACCESS_COARSE_LOCATION, ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, READ_PHONE_STATE, READ_CONTACTS,
//            READ_CALL_LOG, ACCESS_NETWORK_STATE, SYSTEM_ALERT_WINDOW, REQUEST_INSTALL_PACKAGES};
//    private static ArrayList<String> permissionList = new ArrayList<>();
//
//    //检查权限
//    public static void checkPermissions(Activity activity) {
//        for (int i = 0; i < permissions.length; i++) {
//            checkPermission(activity,permissions[i]);
//        }
//        if (permissionList.size() > 0) {
//            String[] permissions = new String[permissionList.size()];
//            for (int i = 0; i < permissionList.size(); i++) {
//                permissions[i] = permissionList.get(i);
//            }
//            //申请权限
//            ActivityCompat.requestPermissions((Activity) activity, permissions, REQUEST_WRITE_EXTERNAL_STORAGE);
//        }
//    }
//
//    private  static void checkPermission(Activity activity,String permission) {
//        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
//        if (ActivityCompat.checkSelfPermission(activity, permission)
//                != PackageManager.PERMISSION_GRANTED) {
//            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
//            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
//                Toast.makeText(activity, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
//            }
//            permissionList.add(permission);
//        }
//    }

    public static void getCamerPermission(Activity activity){
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            //init(barcodeScannerView, getIntent(), null);
        } else {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.CAMERA}, 1);//1 can be another integer
        }
    }


}
